package cn.com.greatwall.flowable.rest;

import java.io.UnsupportedEncodingException;
import java.util.Set;

import org.flowable.engine.ManagementService;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.greatwall.flowable.common.cmd.DeployModelCmd;
import cn.com.greatwall.flowable.common.cmd.SaveModelEditorCmd;
import cn.com.greatwall.flowable.service.ModelService;
import cn.com.greatwall.flowable.service.dto.ModelDto;
import cn.com.greatwall.flowable.service.dto.criteria.ModelQueryCriteria;
import cn.com.greatwall.logging.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * @Author herw
 * @Time 2020-11-13 18:44:02
 * @Version 1.0
 * @Description: TODO(用一句话描述该文件做什么)
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "Flowable：模型管理")
@RequestMapping("/api/flowable/model")
public class ModelController {
    private final ModelService modelService;
    private final ManagementService managementService;
    
    @Log("查询模型")
    @ApiOperation("查询模型")
    @GetMapping
    @PreAuthorize("@el.check('model:list')")
    public ResponseEntity<Object> query(ModelQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(modelService.queryAll(criteria,pageable),HttpStatus.OK);
    }
    
    @Log("查询模型")
    @ApiOperation("查询表配置")
    @GetMapping(value = "/{id}")
    @PreAuthorize("@el.check('model:list')")
    public ResponseEntity<Object> query(@PathVariable String id) throws UnsupportedEncodingException{
        return new ResponseEntity<>(modelService.findById(id), HttpStatus.OK);
    }
    
    @Log("新增模型")
    @ApiOperation("新增模型")
    @PostMapping
    @PreAuthorize("@el.check('model:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ModelDto resources){
        modelService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
    
    @Log("修改模型")
    @ApiOperation("修改模型")
    @PutMapping
    @PreAuthorize("@el.check('model:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ModelDto resources){
        modelService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
    
    @Log("保存流程设计")
    @ApiOperation("保存流程设计")
    @PutMapping(value = "/updateModelEditorSource")
    @PreAuthorize("@el.check('model:edit')")
    public ResponseEntity<Object> updateModelEditorSource(@Validated @RequestBody ModelDto resources){
        managementService.executeCommand(new SaveModelEditorCmd(resources.getId(), resources.getEditor()));
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
    
    @Log("删除模型")
    @ApiOperation("删除模型")
    @DeleteMapping
    @PreAuthorize("@el.check('model:del')")
    public ResponseEntity<Object> delete(@RequestBody Set<String> ids) {
        modelService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    
    @Log(value = "部署流程模型")
    @ApiOperation("部署流程模型")
    @PostMapping(value = "/deploy")
    @PreAuthorize("@el.check('model:add')")
    public ResponseEntity<Object> deployModel(@Validated @RequestBody ModelDto resources) {
        managementService.executeCommand(new DeployModelCmd(resources.getId()));
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
